package com.example.picturegame;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;


/**
 * 总结:
 * 1.自定义控件选择,九宫格,RelativeLayout, id+Rule
 * 2.切图
 * 3.动画图层
 * 4.pause resume restart
 * 5.游戏时间 Handler sendMessageDelayed() 延迟一秒发送线程
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Toolbar toolbar;
    private PuzzleLayoutView puzzleLayoutView;
    private TextView mLevel, mTime,time1;
    private ImageView playOrPause;
    private Button restart;
    private Button change;
    private Button hint;
    private ImageView showimg;
    boolean isChanged = false;
    private TextView idLevel;
    private DrawerLayout drawerLayout;
    int tishi=0;
    private Button rank;

    Intent intent = null;
   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
//        MainActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        playOrPause =findViewById(R.id.play_or_pause);
        mLevel = this.findViewById(R.id.id_level);
        mTime = this.findViewById(R.id.id_time);
        time1=this.findViewById(R.id.id_time1);
        puzzleLayoutView = this.findViewById(R.id.puzzle_layout_view);
        puzzleLayoutView.setTimeEnabled(true);
        change = (Button) findViewById(R.id.change);
        hint = (Button) findViewById(R.id.hint);
        rank=(Button)findViewById(R.id.rank);
        idLevel = (TextView) findViewById(R.id.id_level);

        playOrPause.setOnClickListener(this);
        change.setOnClickListener(this);
        hint.setOnClickListener(this);
        restart = (Button) findViewById(R.id.restart);
        restart.setOnClickListener(this);

        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("拼图游戏");
        setSupportActionBar(toolbar);
        showimg = (ImageView) findViewById(R.id.showimg);
        rank.setOnClickListener(this);
        //监听事件
        puzzleLayoutView.setOnGamePintuListner(new PuzzleLayoutView.GamePintuListner() {
            public void timechanged(int currentTime) {
                //此处为int 注意加""
                mTime.setText(currentTime+"秒");
                time1.setText(currentTime+"");
            }

            public void nextLevel(final int nextLevel) {
                //弹出提示框
                new AlertDialog.Builder(MainActivity.this).setTitle("游戏信息")
                        .setMessage("游戏升级").setPositiveButton("进入下一关",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //游戏结束后,调用下一关
                                puzzleLayoutView.nextLevel();
                                mLevel.setText("第" + +nextLevel + "关");

                            }
                        }).show();
            }

            public void gameover() {
                //弹出提示框
                new AlertDialog.Builder(MainActivity.this).setTitle("游戏信息")
                        .setMessage("游戏结束!").setPositiveButton("继续该关卡",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                puzzleLayoutView.restart();//重新启动
                            }
                        }).setNegativeButton("退出游戏!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).show();
            }

        });


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.help_item:
                    new AlertDialog.Builder(MainActivity.this).setTitle("游戏介绍")
                            .setMessage("该游戏为拼图游戏，分为三种模式\n新手模式要求在80秒内完成3*3的拼图任务\n普通模式要求在120秒内完成4*4的拼图任务\n地狱模式要求在150秒内完成拼图任务\n游戏操作方法：点击要交换的两张图片。")
                            .setPositiveButton("知道了",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                        }
                                    }).show();
                    break;
                case R.id.level1_item:
                    puzzleLayoutView.setLevel(1);
                    puzzleLayoutView.setmTime(80);
                    puzzleLayoutView.setmColumn(3);
                    puzzleLayoutView.pauseGame();
                    puzzleLayoutView.restart(1,puzzleLayoutView.getmTime(),puzzleLayoutView.getmColumn());
                    idLevel.setText("1");
                    Toast.makeText(MainActivity.this,"新手模式",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.level2_item:
                    puzzleLayoutView.setLevel(2);
                    puzzleLayoutView.setmTime(120);
                    puzzleLayoutView.setmColumn(4);
                    puzzleLayoutView.pauseGame();
                    puzzleLayoutView.restart(2,puzzleLayoutView.getmTime(),puzzleLayoutView.getmColumn());
                    idLevel.setText("2");
                    Toast.makeText(MainActivity.this,"普通模式",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.level3_item:
                    puzzleLayoutView.setLevel(3);
                    puzzleLayoutView.setmTime(150);
                    puzzleLayoutView.setmColumn(5);
                    puzzleLayoutView.pauseGame();
                    idLevel.setText("3");
                    puzzleLayoutView.restart(3,puzzleLayoutView.getmTime(),puzzleLayoutView.getmColumn());

                    Toast.makeText(MainActivity.this,"地狱模式",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    //do nothing
            }
            return super.onContextItemSelected(item);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.restart:
                puzzleLayoutView.pauseGame();
                puzzleLayoutView.restart();
            break;
            case R.id.change:
                puzzleLayoutView.pauseGame();
                puzzleLayoutView.changeImg();
            switch (puzzleLayoutView.fimg){
                case 0:showimg.setImageResource(R.drawable.pic_view);break;
                case 1:showimg.setImageResource(R.drawable.yzzz);break;
                case 2:showimg.setImageResource(R.drawable.yzzz1);break;
                case 3:showimg.setImageResource(R.drawable.yzzz2);break;
            }
            puzzleLayoutView.restart();
            break;
            case R.id.play_or_pause:
                if(isChanged==true){
                    playOrPause.setImageResource(R.drawable.ic_pause);
                    puzzleLayoutView.resumeGame();
                    isChanged=false;
                }
                else {
                    playOrPause.setImageResource(R.drawable.ic_play);
                    puzzleLayoutView.pauseGame();
                    new AlertDialog.Builder(MainActivity.this).setTitle("游戏介绍")
                            .setMessage("游戏已经暂停了。")
                            .setPositiveButton("知道了",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                        }
                                    }).show();
                    isChanged=true;
                }
                break;
            case R.id.hint:
                tishi++;
                if(tishi==1) {
                    if (puzzleLayoutView.getLevel() == 1) {
                        Toast.makeText(MainActivity.this, "不是吧不是吧，这么简单都有人不会？", Toast.LENGTH_SHORT).show();
                    }
                    puzzleLayoutView.hint();
                }
                else{
                    puzzleLayoutView.dehint();
                    tishi=0;
                }
                break;
            case R.id.rank:
                int t = Integer.parseInt(time1.getText().toString());

                intent=new Intent(MainActivity.this,ShowActivity.class);
                startActivityForResult(intent,1);
                break;

        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}